import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_cos, y_sin = np.cos(x), np.sin(x)
plt.plot(x, y_cos, color='blue', linewidth=1.0, label='COS', alpha=0.5)
plt.plot(x, y_sin, color='red', linewidth=1.0, label='SIN')
plt.title('Cos & Sin')
plt.xticks([-np.pi, -np.pi / 2.0, 0, np.pi / 2.0, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.yticks(np.linspace(-1, 1, 5, endpoint=True))
plt.legend(loc='upper left')
plt.grid()
# 填充两条线间的颜色
plt.fill_between(x, np.abs(x) < 0.5, y_cos, y_cos > 0.5,
color='green', alpha=0.25)

# 加注释文本
plt.annotate('cos(1)', xy=(1, np.cos(1)), xycoords='data', xytext=(10, 30),
             textcoords='offset points', arrowprops=dict(arrowstyle='->'))
plt.show()

